package Hot100;

import java.util.HashMap;
import java.util.Map;

public class 无重复字符的最长子串 {
    public int lengthOfLongestSubstring(String s) {
        if (s.isEmpty()) return 0;
        char[] charArray = s.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        int lenMax = 0;
        int left = 0;
        for (int right = 0; right < charArray.length; right++) {
            if (map.containsKey(charArray[right]) && left <= map.get(charArray[right]) + 1)
                left = map.get(charArray[right]) + 1;
            map.put(charArray[right], right);
            lenMax = Math.max(lenMax, right - left + 1);
        }
        return lenMax;
    }
}
